<template>
  <div class="page">
    <v-pageTitle title="人员管理"></v-pageTitle>
    <v-pageSearch>
      <el-form :inline="true">
         <el-form-item>
           <el-button  type="primary" @click="batchAdd" size="small">新增人员</el-button>
         </el-form-item>
      </el-form>
    </v-pageSearch>
    <!--工具条-->
    <!--<v-pageToolbar>
        <el-button  type="danger" size="small" @click="batchClick" :disabled="this.sels.length===0">批量删除</el-button>
    </v-pageToolbar>-->
    <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="center">
      <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" @selection-change="selsChange" :default-sort="{prop: 'date', order: 'descending'}">
          <el-table-column fixed type="selection" width="50">
          </el-table-column>
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="120" sortable>
          </el-table-column>
          <el-table-column prop="userName" label="用户名" min-width="120" sortable>
          </el-table-column>
          <el-table-column prop="gender" label="性别" width="100" sortable>
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.gender | genderFilterType">{{scope.row.gender | genderFilter}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="tel" label="手机号" width="150" sortable>
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="180" sortable>
          </el-table-column>
          <el-table-column prop="passWord" label="密码" width="120" sortable>
          </el-table-column>
          <el-table-column prop="branch" label="所属部门" width="120" sortable>
          </el-table-column>
          <el-table-column prop="post" label="岗位" min-width="150" sortable>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="100" sortable>
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status | statusFilterType">{{scope.row.status | statusFilter}}</el-tag>
            </template>
          </el-table-column>
          <!--<el-table-column v-for="(item,index) in tableData.head" :prop="item.key" :label="item.name" sortable :key="index"  min-width="150">
          </el-table-column>-->
          <el-table-column fixed="right" label="操作" min-width="150">
              <template slot-scope="scope">
                <el-button  @click="batchEdit(scope.row)" type="primary" size="small">编辑</el-button>
                <el-button  @click="handleDel(scope.$index, scope.row)" type="danger" size="small">删除</el-button>
              </template>
          </el-table-column>
      </el-table>
    </v-pageTable>

    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="ruleForm.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input v-model="ruleForm.tel"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="branch">
          <!--<el-input v-model="ruleForm.branch"></el-input>-->
          <el-select v-model="ruleForm.branch" filterable placeholder="请选择">
            <el-option
              v-for="item in branch_options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位" prop="post">
          <!--<el-input v-model="ruleForm.post"></el-input>-->
          <el-select v-model="ruleForm.post" filterable placeholder="请选择">
            <el-option
              v-for="item in post_options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="ruleForm.status"
            on-color="#20A0FF"
            off-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input type="textarea" v-model="ruleForm.mark"></el-input>
        </el-form-item>
        <el-form-item label="当前时间">
          <h3 style="font-weight:400;">{{nowDate}}</h3>
        </el-form-item>
        <el-form-item>
            <el-button  type="primary" @click.native="submitForm('ruleForm')" :loading="editLoading">提交</el-button>
            <el-button  @click.native="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import util from '../../../util/util'
  export default {
    data() {
      // 验证手机号码
      var checkTel = (rule, value, callback) => {
        if(value){
          setTimeout(() => {
            if (!(/^1[34578]\d{9}$/.test(value))) {
              callback(new Error('请填写正确的手机号码格式'));
            } else {
              callback();
            }
          }, 100);
        }else{
          callback();
        }
      };
      // 验证邮箱
      var checkEmail = (rule, value, callback) => {
        if(value){
          setTimeout(() => {
            if (!(/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g.test(value))) {
              callback(new Error('请填写正确的邮箱格式'));
            } else {
              callback();
            }
          }, 100);
        }else{
          callback();
        }
      };
      
      return {
        tableData: {
            loading: true,
            head: [],
            body: []
        },
        paginationTotal: 100,
        sels:[],

        //编辑界面数据
				ruleForm: {
          userName: '',
          name: '',
          gender: 0,
          tel: '',
          email: '',
          branch: '',
          post: '',
          status: false,
          mark: ''
        },
        branch_options: [
          {
            value: '新业务部',
            label: '新业务部'
          }, {
            value: '选项2',
            label: '双皮奶'
          }, {
            value: '选项3',
            label: '蚵仔煎'
          }, {
            value: '选项4',
            label: '龙须面'
          }, {
            value: '选项5',
            label: '北京烤鸭'
          }],
          post_options: [{
            value: '前端开发工程师',
            label: '前端开发工程师'
          }, {
            value: '选项2',
            label: '双皮奶'
          }, {
            value: '选项3',
            label: '蚵仔煎'
          }, {
            value: '选项4',
            label: '龙须面'
          }, {
            value: '选项5',
            label: '北京烤鸭'
          }
        ],
        rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          gender: [
            { type: 'number', required: false, message: '请选择性别', trigger: 'blur' }
          ],
          tel: [
            { required: false, validator: checkTel, trigger: 'change' }
          ],
          email: [
            { required: false, validator: checkEmail, trigger: 'change' }
          ],
          branch: [
            { required: true, message: '请选择所属部门', trigger: 'blur' }
          ],
          post: [
            { required: true, message: '请选择岗位', trigger: 'blur' }
          ],
          mark: [
            { required: false, message: '请填写备注', trigger: 'blur' }
          ]
        },
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,

        nowDate: util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
      }
    },
    filters: {
      statusFilter(statue) {
        let sta = statue ? 1 : 0;
        let statusMap = ['禁用','启用']
        return statusMap[sta]
      },
      statusFilterType(statue) {
        let sta = statue ? 1 : 0;
        let statusMapType = ['success','error']
        return statusMapType[sta]
      },
      genderFilter(statue) {
        let statusMap = ['未知','男','女']
        return statusMap[statue]
      },
      genderFilterType(statue) {
        let statusMapType = ['primary','error','success']
        return statusMapType[statue]
      }
    },
    methods: {
      getTableData(data) {
          // ajax 查询表格数据
          this.tableData.head = [
              {
                  key: 'name',
                  name: '姓名'
              },
              {
                  key: 'userName',
                  name: '用户名'
              },
              {
                  key: 'gender',
                  name: '性别'
              },
              {
                  key: 'tel',
                  name: '手机号'
              },
              {
                  key: 'email',
                  name: '邮箱'
              },
              {
                  key: 'passWord',
                  name: '密码'
              },
              {
                  key: 'branch',
                  name: '所属部门'
              },
              {
                  key: 'post',
                  name: '岗位'
              },
              {
                  key: 'status',
                  name: '状态'
              }
          ]
          this.tableData.body = [
            {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '司琳',
              userName: 'Jesen',
              gender: 2,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: true
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }, {
              name: '张宗昭',
              userName: 'Jesen',
              gender: 1,
              tel: '13724568432',
              email: '1234534@qq.com',
              passWord: '123456',
              branch: '新业务部',
              post: '前端开发工程师',
              status: false
            }
          ]
          // 模拟加载
          setTimeout(() => {
              this.tableData.loading = false
          }, 1000)
      },
      sizeChange(val){
        console.log(val)
        this.getTableData()
      },
      pageChange(val){
        console.log(val)
        this.getTableData()
      },
      selsChange: function (sels) {
				this.sels = sels;
			},
      handleClick(row) {
        console.log(row);
      },
      //显示新增界面
      batchAdd(){
        this.editFormVisible = true;
      },
      //显示编辑界面
			batchEdit: function (row) {
				this.editFormVisible = true;
				this.ruleForm = Object.assign({}, row);
			},
      batchClick(){
        // var ids = this.sels.map(item => item.id).toString();
				console.log(this.sels)
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
            this.editFormVisible = false;
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      //删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该员工吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
    },
    watch: {
      //第一种写法 适用于普通变量（简单类型的值的观测写法）
      // 关闭dialog的时候 初始化页面表单数据
      editFormVisible: function (val, oldVal) {
        console.log('new: %s, old: %s', val, oldVal)
        if(!val){
          console.log(this.ruleForm)
          this.ruleForm = {
            userName: '',
            name: '',
            gender: 0,
            tel: '',
            email: '',
            branch: '',
            post: '',
            status: false,
            mark: ''
          }
        }
      }
    },
    mounted() {
        // 获取table数据
        this.getTableData()

        let  _this = this;
        this.timer = setInterval(function(){
            _this.nowDate = util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
        },1000);
    },
    beforeDestory(){
        if(this.timer){
            clearInterval(this.timer)
        }
    }
  }
</script>